package com.example.medispider.service.master;

import com.example.medispider.pojo.master.MasterApiResponse;
import com.example.medispider.pojo.master.MasterPageBean;
import com.example.medispider.pojo.master.MasterResponseData;
import com.example.medispider.pojo.master.SettlementMaster;
import com.example.medispider.utils.PageRequestBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

@Service
public class MasterPageProcessor implements PageProcessor {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private final Site site;

    @Autowired
    public MasterPageProcessor(Site site) {
        this.site = site;
    }

    @Override
    public void process(Page page) {
        try {
            // 获取JSON响应
            String jsonResponse = page.getRawText();
            // 解析JSON到Java对象
            MasterApiResponse masterApiResponse = objectMapper.readValue(jsonResponse, MasterApiResponse.class);
            // 检查请求是否成功
            if (masterApiResponse.isSuccess()) {
                MasterResponseData masterResponseData = masterApiResponse.getData();
                if (masterResponseData != null && masterResponseData.getPageBean() != null) {
                    MasterPageBean masterPageBean = masterResponseData.getPageBean();
                    List<SettlementMaster> masters = masterPageBean.getData();
                    if (masters != null && !masters.isEmpty()) {
                        // 将数据放入page，供Pipeline处理
                        page.putField("masters", masters);
                        // 首次请求，添加后续分页请求
                        if (masterPageBean.getFirstPage() != null && masterPageBean.getFirstPage()) {
                            Integer pages = masterPageBean.getPages(); //总页数
                            if (pages > 1) {
                                // 获取当前请求的Request对象
                                Request currentRequest = page.getRequest();
                                ArrayList<Request> pageRequests = PageRequestBuilder.createPageRequests(currentRequest, pages);
                                for (Request pageRequest : pageRequests) {
                                    page.addTargetRequest(pageRequest);
                                }
                            }
                        }
                    } else {
                        String body = new String(page.getRequest().getRequestBody().getBody());
                        System.err.println("主单数据分页查找为空：" + body);
                        page.setSkip(true); // 跳过这个页面的处理
                    }
                } else {
                    System.err.println("主单响应数据为空：MasterResponseData");
                    page.setSkip(true); // 跳过这个页面的处理
                }
            } else {
                // 处理错误情况
                System.err.println("主单API请求失败: " + masterApiResponse.getMessage());
                page.setSkip(true); // 跳过这个页面的处理
            }
        } catch (Exception e) {
            System.err.println("请求失败: " + e.getMessage());
            page.setSkip(true);
        }
    }


    @Override
    public Site getSite() {
        return site;
    }

}
